use strict;
use utf8;
use Math::Complex;

binmode(STDIN, ":encoding(utf-8)");
binmode(STDOUT, ":encoding(utf-8)");
binmode(STDERR, ":encoding(utf-8)");

my %w2nu;
my $nusers = 0;
open(F, "word2nuser.txt");
binmode(F, ":encoding(utf-8)");
while (<F>) {
  chomp $_;
  my ($w, $n) = split(/\t/, $_);
  $w2nu{$w} = $n;
  if ($n > $nusers) {
    $nusers = $n;
  }
}
close(F);

foreach my $k (keys %w2nu) {
  $w2nu{$k} = $w2nu{$k} / $nusers;
}

#my $ctrlkw = "контрольноесловооднонатысячу";
my $ctrlkw = "###TOTAL###";
my %h;

while (<STDIN>) {
  chomp $_;
  my ($key, $c1, $d1, $c2, $d2) = split(/\s+/, $_);
  @{$h{$key}} = ($c1, $d1, $c2, $d2);
}

my $total_docs = $h{$ctrlkw}->[1] + $h{$ctrlkw}->[3];
my $total_docs_c1 = $h{$ctrlkw}->[1];
my $total_docs_c2 = $h{$ctrlkw}->[3];

foreach my $k (keys %h) {
  if ($k eq $ctrlkw) {
    next;
  }
  my @a = @{$h{$k}};
  my $c1_neg = $total_docs_c1 - $a[1] + 1;
  my $c2_neg = $total_docs_c2 - $a[3] + 1;
#  print STDERR $k . "\n";
  my $mi = 
      $a[1]/$total_docs * logn($total_docs*$a[1]/(($a[1] + $a[3])*$total_docs_c1), 2)
    + $c1_neg/$total_docs * logn($total_docs*$c1_neg/(($c1_neg + $c2_neg)*$total_docs_c1), 2)
    + $a[3]/$total_docs * logn($total_docs*$a[3]/(($a[1] + $a[3])*$total_docs_c2), 2)
    + $c2_neg/$total_docs * logn($total_docs*$c2_neg/(($c1_neg + $c2_neg)*$total_docs_c2), 2);
  push @{$h{$k}}, $mi;
}

foreach my $k (sort { @{$h{$b}}->[4] <=> @{$h{$a}}->[4] } keys %h) {
  if ($w2nu{$k} > 0.8) { next; }
  if (length($k) < 2) { next; }
  print $k . "\t" . join("\t", @{$h{$k}}) . "\t" . $w2nu{$k} . "\n";
}
